package com.qf.controller;

import com.qf.constant.CacheConstants;
import com.qf.enums.InterfaceExceptionEnums;
import com.qf.feign.CacheFeign;
import com.qf.form.SmsInterfaceForm;
import com.qf.service.InterfaceService;
import com.qf.util.Result;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/*** @author xl
 * @version V1.0
 * @Project JavaGP12
 * @Package com.qf.controller
 * @Description: ${TODO}
 *@Date 2022/8/24 15:23
 */
@RestController
@RequestMapping("/sms")
public class InterfaceController {

    @Autowired
    private CacheFeign cacheFeign;

    @Value("${ip-header}")
    private String[] ipHeaders;

    @Autowired
    private InterfaceService interfaceService;

    @PostMapping("/interface")
    public Object interfaceFrom(SmsInterfaceForm smsInterfaceForm, HttpServletRequest request) {
        //客户数据的key   Client:1  作为key
        String key = CacheConstants.CACHE_PREFIX_CLIENT + smsInterfaceForm.getClientID();
        Map user = cacheFeign.hMGet(key);
        //1.判断当前用户是否存在
        if (null == user || user.size() == 0) {
            return Result.error(InterfaceExceptionEnums.INTERFACE_CLIENT_ID_ERROR);
        }
        //2.判断当前用户密码是否正确
        Object p = user.get("pwd");
        String pwd = smsInterfaceForm.getPwd();
        if (StringUtils.isEmpty(pwd) || !pwd.equals(p)) {
            return Result.error(InterfaceExceptionEnums.INTERFACE_PWD_ERROR);
        }
        //3.校验短信内容长度
        String content = smsInterfaceForm.getContent();
        if (StringUtils.isEmpty(content) || content.length() > 500) {
            return Result.error(InterfaceExceptionEnums.INTERFACE_CONTENT_ERROR);
        }
        //4.下行编号
        String srcID = smsInterfaceForm.getSrcID();
        if (StringUtils.isEmpty(srcID)) {
            return Result.error(InterfaceExceptionEnums.INTERFACE_SRC_ID_ERROR);
        }
        //5.手机号校验
        String mobile = smsInterfaceForm.getMobile();
        if (StringUtils.isEmpty(mobile)) {
            return Result.error(InterfaceExceptionEnums.INTERFACE_MOBILE_ERROR);
        }
        String[] m = mobile.split(",");
        //手机号的正则表达式
        String regex = "^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$";
        List<String> errorPhone = new ArrayList<>();
        if (null != m && m.length > 0) {
            for (String phone : m) {
                if (!phone.matches(regex)) {
                    errorPhone.add(phone);
                }
            }
        }
        if (errorPhone.size() > 0) {
            return Result.error(105, "手机号错误:[" + errorPhone.toString() + "]");
        }
        //6.校验IP
        String ip = getIP(request);
        smsInterfaceForm.setIp(ip);
        String ipaddress = (String) user.get("ipaddress");
        boolean flag = false;
        if (!StringUtils.isEmpty(ipaddress)) {
            String[] ips = ipaddress.split(",");
            for (String s : ips) {
                if (s.equals(ip)) {
                    flag = true;
                    break;
                }
            }
        }
        if (!flag) {  //ip在客户指定的IP中肯定 有一个是相同的
            return Result.error(InterfaceExceptionEnums.INTERFACE_IP_ERROR+ errorPhone.toString());
        }

        //Service层
        interfaceService.submitInterfaceForm(smsInterfaceForm);

        //全部正确返回ok
        return Result.ok();

    }


    //获取代理 或代理的IP
    public String getIP(HttpServletRequest request) {
        for (String ipHeader : ipHeaders) {
            String ip = request.getHeader(ipHeader);
            if (!StringUtils.isEmpty(ip) && !"unknown".equalsIgnoreCase(ip)) {
                //请求头的ip
                return ip;
            }
        }
        //没有代理
        return request.getRemoteAddr();
    }


}
